using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Beta_fundies_controls_publicadmin_SingleQuote : Controls.DataManagerQuoteControl
{
    protected override void ItemChanged()
    {
        Refresh();
    }

    public void Refresh()
    {
        if (CurrentQuote != null)
        {
            mviewQuote.SetActiveView(viewQuote);

            lblQuote.Text = DAL.YCodes.Format(CurrentQuote.FundieQuote);
            lblFundie.Text = Helpers.StringHelper.SafeFormat(CurrentQuote.Fundie);
            lblArchive.Text = Helpers.StringHelper.SafeFormat(CurrentQuote.Archive.Title);

            lnkUrl.Text = Helpers.StringHelper.SafeFormat(CurrentQuote.Board);
            if (Uri.IsWellFormedUriString(CurrentQuote.URL, UriKind.Absolute))
            {
                lnkUrl.NavigateUrl = CurrentQuote.URL;
            }
            else
            {
                lnkUrl.NavigateUrl = "#Bad_URL";
            }

            pnlMod.Visible = CurrentUser != null && CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator);
        }
        else
        {
            mviewQuote.SetActiveView(viewError);
        }
    }



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            NewQuote();
        }
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        NewQuote();
    }

    private DAL.User CurrentUser
    {
        get
        {
            return Managers.UserLoginManager.CurrentUser;
        }
    }

    private void NewQuote()
    {
        CurrentQuote = DAL.Quotes.GetRandomUnapprovedQuote(CurrentUser);
    }

    protected void btnApprove_Click(object sender, EventArgs e)
    {
        if (CurrentUser != null)
        {
            AddPublicAdmin(true);

            // votes from untrusted users are not tallied
            if (CurrentUser.AdminSettings.AllowPublicAdmin && CurrentUser.TotalPosts >= Constants.PublicAdminSettings.MinimumRequiredPosts)
            {
                CurrentQuote.ApproveVotes += 1;
                if (CurrentQuote.ApproveVotes >= Constants.PublicAdminSettings.MinimumApproveVotes)
                {
                    CurrentQuote.ApprovalStatus = DAL.Quote.QuoteStatus.Approved;
                    CurrentQuote.DatePosted = DateTime.Now;

                    CurrentUser.FateSealer += 1;
                    DAL.Users.Save(CurrentUser);
                    lblMsg.Text = "T3h rox0rz! You've approved a quote!";
                }

                DAL.Quotes.Save(CurrentQuote);
            }

            NewQuote();
        }
    }

    private void AddPublicAdmin(bool approved)
    {
        DAL.PublicAdmin myPA = new DAL.PublicAdmin();
        myPA.Approved = approved;
        myPA.User = CurrentUser;
        myPA.Quote = CurrentQuote;

        DAL.PublicAdmins.Add(myPA);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (CurrentUser != null)
        {
            AddPublicAdmin(false);

            // votes from untrusted users are not tallied
            if (CurrentUser.AdminSettings.AllowPublicAdmin && CurrentUser.TotalPosts >= Constants.PublicAdminSettings.MinimumRequiredPosts)
            {
                CurrentQuote.DeleteVotes += 1;
                if (CurrentQuote.DeleteVotes >= Constants.PublicAdminSettings.MinimumDeleteVotes)
                {
                    CurrentQuote.ApprovalStatus = DAL.Quote.QuoteStatus.Deleted;

                    CurrentUser.FateSealer += 1;
                    DAL.Users.Save(CurrentUser);
                    lblMsg.Text = "W00t w00t! You've just eviscerated a quote!";
                }

                DAL.Quotes.Save(CurrentQuote);
            }

            NewQuote();
        }
    }

    protected void btnSpeedyApprove_Click(object sender, EventArgs e)
    {
        if (CurrentUser != null && CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator))
        {
            CurrentQuote.DatePosted = DateTime.Now;
            CurrentQuote.ApprovalStatus = DAL.Quote.QuoteStatus.Approved;
            DAL.Quotes.Save(CurrentQuote);
        }
        NewQuote();
    }
    protected void btnSpeedyDelete_Click(object sender, EventArgs e)
    {
        if (CurrentUser != null && CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator))
        {
            CurrentQuote.ApprovalStatus = DAL.Quote.QuoteStatus.Deleted;
            DAL.Quotes.Save(CurrentQuote);
        }
        NewQuote();
    }
}
